﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class AddLoanForm : Form
    {
        private ArrayList id_rateCredit = new ArrayList();
        private ArrayList titleCredit = new ArrayList();
        private ArrayList procentCredit = new ArrayList();
        private ArrayList periodCredit = new ArrayList();
        string client_type;
        OracleConnection connection;
        bool flag = false;
        int id_client;
        LoanForm loanFr;

        public AddLoanForm(string clientType, LoanForm Fr, int idClient)
        {
            id_client = idClient;
            client_type = clientType;
            loanFr = Fr;
            InitializeComponent();
        }

        private void SelectFromCredit(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selRate = new ClassRateType();
            selRate = dbWork.SelectFromCreditRate(client_type);
            int j = 0;
            if (selRate.id_rate.Count == 0) { dataGridView1.RowCount = 1; } else { dataGridView1.RowCount = selRate.id_rate.Count; }
            while (j < selRate.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selRate.title[j].ToString());
                dataGridView1[1, j].Value = selRate.procent[j].ToString();
                dataGridView1[2, j].Value = selRate.period[j].ToString();
                j++;
            }
            
        }
        
        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int credit = Convert.ToInt32(textBoxCredit.Text);
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.InsertFromLoan(dateTimePicker1.Value, period, credit, procent, client_type, id_client);
                loanFr.RefreshTable();
                this.Close();
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных"); }

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddLoanForm_Load(object sender, EventArgs e)
        {
            this.SelectFromCredit(client_type);
        }

        private void textBoxCredit_Enter(object sender, EventArgs e)
        {

        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxCredit.Text.Equals("") == false)
                {
                    int index;
                    LogicClass logic = new LogicClass();
                    index = dataGridView1.CurrentRow.Index;
                    int credit = logic.GetCreditSumm(Convert.ToInt32(textBoxCredit.Text), Convert.ToDouble(dataGridView1[1, index].Value));
                    labelInfo.Text = credit.ToString();
                }
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных"); }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            

        }
    }
}
